package com.jspgou.cms.manager.impl;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jspgou.cms.dao.AftersaleDao;
import com.jspgou.cms.dao.AftersaleDemandDao;
import com.jspgou.cms.dao.AftersaleTypeDao;
import com.jspgou.cms.dao.OrderDao;
import com.jspgou.cms.dao.ShopMemberDao;
import com.jspgou.cms.entity.Aftersale;
import com.jspgou.cms.entity.AftersaleDemand;
import com.jspgou.cms.entity.AftersaleType;
import com.jspgou.cms.entity.Order;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.manager.AftersaleMng;
import com.jspgou.common.hibernate3.Updater;
import com.jspgou.common.page.Pagination;
import com.jspgou.core.entity.Website;

@Service
@Transactional
public class AftersaleMngImpl implements AftersaleMng {
	@Transactional(readOnly = true)
		public Pagination getPage(Long memberId,Long storeId,Long typeId,
				Long demandId,int pageNo, int pageSize){
		Pagination page = dao.getPage(memberId,storeId,typeId,demandId,pageNo, pageSize);
		return page;
	}

	@Transactional(readOnly = true)
	public Aftersale findById(Long id) {
		Aftersale entity = dao.findById(id);
		return entity;
	}

	public Aftersale save(Aftersale bean) {
		dao.save(bean);
		return bean;
	}
	
	public Aftersale save(Aftersale bean,Website web,Long typeId,
			Long demandId,Long orderId,Long memberId,String[] fashionPic){
		AftersaleType type=aftersaleTypeDao.findById(typeId);
		AftersaleDemand demand=aftersaleDemandDao.findById(demandId);
		ShopMember member=shopMemberDao.findById(memberId);
		Order order=orderDao.findById(orderId);
		bean.setDemand(demand);
		bean.setType(type);
		bean.setOrder(order);
		if(order.getStore()!=null){			
			bean.setStore(order.getStore());
		}
		bean.setApplyTime(new Date());
		bean.setWebsite(web);
		bean.setMember(member);
		dao.save(bean);
		// 保存图片/大图片/放大图片集
		if (fashionPic != null && fashionPic.length > 0) {
			for (int i = 0, len = fashionPic.length; i < len; i++) {
				if (!StringUtils.isBlank(fashionPic[i])) {
					bean.addToPictures(fashionPic[i]);
				}
			}
		}
		return bean;
	}

	public Aftersale update(Aftersale bean) {
		Updater<Aftersale> updater = new Updater<Aftersale>(bean);
		Aftersale entity = dao.updateByUpdater(updater);
		return entity;
	}
	
	public Aftersale update(Aftersale bean,Website web,Long typeId,Long demandId,
			Long orderId,Long memberId,String[] fashionPic){
		AftersaleType type=aftersaleTypeDao.findById(typeId);
		AftersaleDemand demand=aftersaleDemandDao.findById(demandId);
		ShopMember member=shopMemberDao.findById(memberId);
		Order order=orderDao.findById(orderId);
		bean.setDemand(demand);
		bean.setType(type);
		bean.setOrder(order);
		bean.setStore(order.getStore());
		bean.setApplyTime(new Date());
		bean.setWebsite(web);
		bean.setMember(member);
		Updater<Aftersale> updater=new Updater<Aftersale>(bean);
		bean=dao.updateByUpdater(updater);
		// 保存图片/大图片/放大图片集
		bean.getPictures().clear();
		if (fashionPic != null && fashionPic.length > 0) {
			for (int i = 0, len = fashionPic.length; i < len; i++) {
				if (!StringUtils.isBlank(fashionPic[i])) {
					bean.addToPictures(fashionPic[i]);
				}
			}
		}
		return bean;
	}

	public Aftersale deleteById(Long id) {
		Aftersale bean = dao.deleteById(id);
		return bean;
	}
	
	public Aftersale[] deleteByIds(Long[] ids) {
		Aftersale[] beans = new Aftersale[ids.length];
		for (int i = 0,len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

	private AftersaleDao dao;
	@Autowired
	private AftersaleTypeDao aftersaleTypeDao;
	@Autowired
	private AftersaleDemandDao aftersaleDemandDao;
	@Autowired
	private OrderDao orderDao;
	@Autowired
	private ShopMemberDao shopMemberDao;


	@Autowired
	public void setDao(AftersaleDao dao) {
		this.dao = dao;
	}
}